package cn.zcj.studytest.service;

import cn.zcj.studytest.constant.AuthConstant;
import cn.zcj.studytest.domain.SignUp;
import cn.zcj.studytest.utils.JwtUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class TokenService {


    @Resource
    private RedisTemplate<String, String> redisTemplate;
    @Resource
    private SignUpService signUpService;

    public String getToken(Long userId) {
        SignUp signUp = signUpService.getById(userId);
        String jwtToken = JwtUtil.creatJwt(signUp);
        redisTemplate.opsForValue().set(AuthConstant.TOKEN_CACHE_PATH + jwtToken, String.valueOf(userId), 1, TimeUnit.DAYS);
        return jwtToken;
    }
}
